async function crawlingAllTopAsin() {
  const allItems = await getAllnewReleasesItems();
  const config = await getConfig();
  const needCategories = config.newReleases.split('/\,|，/').map(i => i.trim())
  const items = allItems.filter(i => needCategories.includes(i.category))

  for (let item of items) {
    await crawlingOneCategory(item);
  }
}

async function crawlingOneCategory(item) {
  const category = item.category;
  const urls = [item.url, item.url + '?pg=2'];

  const results = await urls.reduce(async (previousPromise, currentUrl) => {
    const accumulator = await previousPromise;
    const result = await new Promise((resolve, reject) => {
      openUrlAndExecScript(currentUrl, 'apps/crawling_top_asin/content.js', resolve);
    });
    console.log(currentUrl)
    console.log(result)
    accumulator.push(result);
    return accumulator;
  }, Promise.resolve([])); // 初始值为空数组

  const pasins = await crawlingAllParentAsin(results.flat());

  const uploadUrl = 'https://api.miraazzurra.com/amazon/google_sheet/upload_new_release_asins';
  let retryTime = 0;
  while (retryTime < 3) {
    try {
      await post(uploadUrl, { category: category, asins: pasins });
      retryTime = 4; // 成功后退出循环
    } catch (error) {
      retryTime++;
      if (retryTime >= 3) {
        console.log('无法获取到 top list 信息')
      }
    }
  }

  console.log(`${category} ASINs:`, pasins);
}

async function getParentAsin(url) {
  if (!url) return '';

  // 定义超时函数
  const timeoutPromise = (timeout) => 
    new Promise((_, reject) => setTimeout(() => reject(new Error('Request timed out')), timeout));

  // 封装 fetch 请求，添加超时逻辑
  async function fetchWithTimeout(url, timeout = 10000) {
    try {
      const response = await Promise.race([
        fetch(url),
        timeoutPromise(timeout)
      ]);
      if (!response.ok) throw new Error(`Response not ok: ${response.statusText}`);
      return response;
    } catch (error) {
      throw error; // 抛出错误，由调用者捕获
    }
  }

  for (let attempt = 1; attempt <= 3; attempt++) {
    try {
      const response = await fetchWithTimeout(url);
      const responseBody = await response.text();
      const match = responseBody.match(/"parentAsin":\s*"([A-Z0-9]{10})"/);
      return match ? match[1] : '';
    } catch (error) {
      console.error(`Attempt ${attempt} failed: ${error.message}`);
      if (attempt === 3) return '';
      // 可选：在尝试之间添加延迟
      await new Promise(resolve => setTimeout(resolve, 1000)); // 1秒的延迟
    }
  }
}

async function crawlingAllParentAsin(asins) {
  const pAsins = []

  for (var i = 0; i < asins.length; i++) {
    console.log(`Current progress: ${i+1}/${asins.length}`)
    await sleepWithAlarms(getRandomInt(2000, 5000))
    let url = (asins[i] == null ? null : `https://www.amazon.com/dp/${asins[i]}`)
    pAsins.push(await getParentAsin(url))
  }

  return pAsins;
}

function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}

async function getAllnewReleasesItems() {
  const configUrl = 'https://api.miraazzurra.com/amazon/google_sheet/content?spreadsheetId=15EQptqudfnDg-K1hzoztit3ZEF0HtPz_9ZYQlDpk0-0&sheetName=榜单及其链接'
  const configTable = await fetch(configUrl).then(response => response.json())

  const titles = configTable[0]
  let items = []
  
  for(let i = 1; i < configTable.length; i++) {
    let row = configTable[i]
    items.push({ category: row[0], url: row[1] })
  }
  return items
}
